/*
 * =====================================================================================
 *
 *       Filename:  cl_channel.hxx
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  03/30/2011 07:29:55 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Chen Ken (Ken), zlchen.ken@gmail.com
 *        Company:  EMC
 *
 * =====================================================================================
 */

#ifndef _CL_CHANNEL_HXX_
#define _CL_CHANNEL_HXX_

#include <poll.h>
#include <vector>
#include "cl_callback.hxx"

namespace neo
{
class CLChannel
{
public:
  explicit CLChannel(int fd, unsigned int events = CL_NONE_EVENT);
  void handleEvent(void);
  void setReadCallback(const CLCallbackFunctor& cb);
  void setWriteCallback(const CLCallbackFunctor& cb);
  void setCloseCallback(const CLCallbackFunctor& cb);
  void setErrorCallback(const CLCallbackFunctor& cb);
  void setReadyEvents(unsigned int readyEvents);
  void enableReading(void);
  void enableWriting(void);
  void disableReading(void);
  void disableWriting(void);
  void disableAll(void);
  bool isWriteEnabled(void) const;
  bool isReadEnabled(void) const;
  unsigned int events(void) const;
  int fd(void) const;

public:
  static const unsigned int CL_READ_EVENT = POLLIN | POLLPRI;
  static const unsigned int CL_WRITE_EVENT = POLLOUT;
  static const unsigned int CL_NONE_EVENT = 0;

private:
  int m_fd;
  unsigned int m_events;
  unsigned int m_readyEvents;
  CLCallbackFunctor m_readCallback;
  CLCallbackFunctor m_writeCallback;
  CLCallbackFunctor m_closeCallback;
  CLCallbackFunctor m_errorCallback;
};

typedef std::vector<CLChannel*> CLChannelList;
} // neo

#endif //_CL_CHANNEL_HXX_

